package com.tongji.diy.model.furniturePlacementOptimization.assembleComplement
{
	import com.tongji.diy.model.furniturePlacementOptimization.CostComputing;
	import com.tongji.diy.model.furniturePlacementOptimization.DataAndFunction;
	import com.tongji.diy.utils.OptimizationMath;
	
	import flash.geom.Vector3D;

	public class NearBase extends FurnitureBase
	{
		private var cc:CostComputing;
		//iteration process 变量
		private var _lastCost:Number = Infinity;
		private var _currentCost:Number = Infinity;
		private var _lastPositionAngle:Vector3D = new Vector3D(0,0,0);//x,y 代表位置，z代表角度
		private var _randNum:Vector3D = new Vector3D;//随机数变量用于记录随机摆放中的随机数，三个参数分别表示x，y，angle
		private var _recorder:Vector3D; //用于记录上次的摆放位置。参数表示同上
		
		public function NearBase(furnitrueIndex:int, furnitureClass:int, aganistWall:Boolean, length:Number=0, width:Number=0, height:Number=0)
		{
			super(furnitrueIndex, furnitureClass, aganistWall, length, width, height);
		}

		public function caculateCost(data:DataAndFunction):Number
		{
			cc = new CostComputing(data);	
			return /*cc.cost_angle(this) +*/ cc.cost_distance(this) /*+  cc.cost_symmetry(this)*/;
		}
		
		public function get lastCost():Number
		{
			OptimizationMath.assert(_lastCost != Infinity);
			return _lastCost;
		}
		
		public function get iterationDirection():Vector3D
		{
			var v:Vector3D = new Vector3D(x - _lastPositionAngle.x, y - _lastPositionAngle.y);
			v.normalize();
			v.z = angle - _lastPositionAngle.z;
			return v; 
		}
		public function get randNum():Vector3D
		{
			return _randNum;
		}
		public function set randNum(rand:Vector3D):void
		{
			this._randNum = rand;
		}
		public function set randNumX(randX:Number):void
		{
			this._randNum.x = randX;
		}	
		public function set randNumY(randY:Number):void
		{
			this._randNum.y = randY
		}
		public function set randNumAngle(randAngle:Number):void
		{
			this._randNum.z = randAngle;
		}
		public function set recorderX(recorderX:Number):void
		{
			this._recorder.x = recorderX;
		}
		public function set recorderY(recorderY:Number):void
		{
			this._recorder.y = recorderY;
		}
		public function set recorderZ(recorderZ:Number):void
		{
			this._recorder.z = recorderZ;
		}
		public function set recorder(recorder:Vector3D):void
		{
			this._recorder = recorder;
		}
		public function get recorder():Vector3D
		{
			return this._recorder;
		}
		
		public function writeRecorder():void
		{
			this._recorder.x = this.x;
			this._recorder.y = this.y;
			this._recorder.z = this.angle;
		}
		public function uploadRecorder():void
		{
			this.x = this._recorder.x;
			this.y = this._recorder.x;
			this.angle = this._recorder.z;
		}
		
		//刷新随机数,产生的随机数是正太随机数
		public function flushRandNum():void
		{
			this._randNum.x = OptimizationMath.normalRandom();
			this._randNum.y = OptimizationMath.normalRandom();
			this._randNum.z = OptimizationMath.normalRandom();
		}
		
		public function set currentCost(cost:Number):void
		{
			this._lastCost = this._currentCost;
			this._currentCost = cost;
		}
		
		public function get currentCost():Number
		{
			return this._currentCost;
		}
		
		public override function set x(NewX:Number):void
		{
			this._lastPositionAngle.x = x ;
			super.x = NewX;
		}
		
		public override function set y(NewY:Number):void
		{
			this._lastPositionAngle.y = y ;
			super.y = NewY;
		}
		
		public override function set angle(NewAngle:Number):void
		{
			this._lastPositionAngle.z = this.angle;
			super.angle = NewAngle;
		}
	}
}